/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clip.gui;

import L.L;
import clip.clipApp;
import java.awt.AWTException;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.TrayIcon.MessageType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;

/**
 *
 * @author admin
 */
public class ClipTrayIcon {
    private SystemTray tray;
    private TrayIcon trayIcon;
    private clipApp app;

    public ClipTrayIcon(clipApp app) {
        this.app = app;
    }

    private MouseListener mouseListener = new MouseListener() {
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                app.clipWin.setVisible(true);
                setVisible(true);
            }
        }

        public void mouseEntered(MouseEvent e) {
        }

        public void mouseExited(MouseEvent e) {
        }

        public void mousePressed(MouseEvent e) {
        }

        public void mouseReleased(MouseEvent e) {
        }

    };
    private ActionListener exitListener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Exiting...");
            System.exit(0);
        }

    };
    private ActionListener showListener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            app.clipWin.setVisible(true);
            setVisible(true);
        }

    };

    private PopupMenu createTrayIconPopupMenu() throws HeadlessException, HeadlessException {
        PopupMenu popupMenu = new PopupMenu();
        MenuItem showItem = new MenuItem("show");
        showItem.addActionListener(showListener);
        popupMenu.add(showItem);
        MenuItem exitItem = new MenuItem("exit");
        exitItem.addActionListener(exitListener);
        popupMenu.add(exitItem);

        return popupMenu;
    }

    public boolean isVisible() {
        if (tray != null) {
            return tray.getTrayIcons().length > 0;
        } else {
            return false;
        }
    }

    public void setVisible(boolean setVisible) {
        if (SystemTray.isSupported()) {
            tray = SystemTray.getSystemTray();
            if (setVisible && !this.isVisible()) {
                Image image = Toolkit.getDefaultToolkit().getImage("src/clip/resources/n.jpg");
                trayIcon = new TrayIcon(image, "netclip3000", createTrayIconPopupMenu());
                trayIcon.setImageAutoSize(true);
                trayIcon.addActionListener(showListener);
                trayIcon.addMouseListener(mouseListener);
                try {
                    tray.add(trayIcon);
                } catch (AWTException e) {
                    L.e("TrayIcon could not be added.");
                }
            } else if (!setVisible && this.isVisible()) {// deaktivieren
                tray.remove(trayIcon);
            } else {
                L.w("hmmmm   bin in if case nr.3");
            }
        } else {            //  System Tray is not supported
            L.e("TrayIcon is not supported !!");
        }
    }

    public void displayBalloonMessage(String s) {
        if (!this.isVisible()) setVisible(true);
        trayIcon.displayMessage("control800", s, MessageType.INFO);
    }

    public void setToolTip(String tooltip) {
        trayIcon.setToolTip(tooltip);
    }

    public static boolean isSupported() {
        return SystemTray.isSupported();
    }

}
